package com.example.console.controller.user;

import com.alibaba.fastjson.JSON;
import com.example.module.module.user.entity.User;
import com.example.module.module.user.service.UserService;
import com.example.module.utils.Response;
import com.example.module.utils.SpringUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/console")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Response login(@RequestParam String phone,
                          @RequestParam String password,
                          HttpSession httpSession) {

        try {
            User user = userService.login(phone, password);
            httpSession.setAttribute("user", user);
            httpSession.setAttribute(SpringUtils.getProperty("application.session.key"), JSON.toJSONString(user));
            return new Response(1001,"登录成功");
        }catch (Exception e) {
            return new Response(4004,e.getMessage());
        }

    }
}
